#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 500010;
int n;
int a[N], tmp[N];
LL merge_sort(int l, int r) {
    if (l == r) return 0;
    int mid = l + r >> 1;
    LL ans = merge_sort(l, mid) + merge_sort(mid + 1, r);
    int i = l, j = mid + 1, k = 0;
    while (i <= mid && j <= r) {
        if (a[i] <= a[j]) tmp[k++] = a[i++];
        else {
            tmp[k++] = a[j++];
            ans += mid - i + 1;
        }
    }
    while (i <= mid) tmp[k++] = a[i++];
    while (j <= r) tmp[k++] = a[j++];
    for (int i = l, j = 0; i <= r; i++, j++) a[i] = tmp[j];
    return ans;
}
int main() {
    while (cin >> n, n) {
        for (int i = 1; i <= n; i++) cin >> a[i];
        cout << merge_sort(1, n) << endl;
    }
    return 0;
}
